package mdt.action;

import org.tigris.gef.base.Editor;
import org.tigris.gef.base.Globals;
import mdt.program.Displayer;
import mdt.gui.TextAttributePanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * Created by xiaobyu on 7/19/2016.
 */
public class SetFigTextAttrAction extends AbstractAction {

    public SetFigTextAttrAction() {
        super("Set Text Attributes");
        putValue(Action.SMALL_ICON, new ImageIcon(FontBoldAction.class.getResource("/resources/SetFigAttr.png")));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JDialog dialog = new TextAttrDialog();
        dialog.pack();
        Point frameLocation = Displayer.instance().getJFrame().getLocation();
        Rectangle selBounds = Globals.curEditor().getSelectionManager().getBounds();
        final Editor ce = Globals.curEditor();
        JComponent drawingPanel = (JPanel) ce.getJComponent();
        Component awtComp = drawingPanel;
        while (!(awtComp instanceof RootPaneContainer) && awtComp != null) {
            awtComp = awtComp.getParent();
        }
        JLayeredPane layeredPane = ((RootPaneContainer) awtComp).getLayeredPane();
        selBounds = SwingUtilities.convertRectangle(drawingPanel, selBounds, layeredPane);
        dialog.setLocation(frameLocation.x + selBounds.x, frameLocation.y + selBounds.y);
        dialog.setVisible(true);
    }

    public class TextAttrDialog extends JDialog {
        public TextAttrDialog() {
            super(Displayer.instance().getJFrame(), ModalityType.DOCUMENT_MODAL);
            Container container = getContentPane();
            container.setLayout(new BorderLayout());
            container.add(new TextAttributePanel(), BorderLayout.CENTER);

            JButton okButton = new JButton(new AbstractAction("OK") {

                @Override
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });

            JPanel buttonPanel = new JPanel();
            buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
            buttonPanel.add(Box.createHorizontalGlue());
            buttonPanel.add(okButton);
            buttonPanel.add(Box.createHorizontalStrut(30));
            buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 15, 0));
            container.add(buttonPanel, BorderLayout.SOUTH);
        }
    }
}
